//
//  WelcomeViewController.swift
//  新浪微博
//
//  Created by seteny on 16/7/12.
//  Copyright © 2016年 seteny. All rights reserved.
//

import UIKit
import SDWebImage


class WelcomeViewController: UIViewController {
    
    // MARK:  -- 懒加载控件
    private lazy var bgImageView: UIImageView = {
        
      let imageView = UIImageView(image: UIImage(named: "ad_background"))
        return imageView
    }()
    
    // 头像
    private lazy var headImageView: UIImageView = {
        
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
       //判断头像地址是否为nil
        if let imageUrl = UserAccountViewModel.shareUserAccount.userAccount?.avatar_large {
            //有头像地址
            imageView.sd_setImageWithURL(NSURL(string: imageUrl), placeholderImage: UIImage(named:"avatar_default_big"))
        }
        
        //设置圆角
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
        return imageView
    }()
    
    
    //消息
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
       label.font = UIFont.systemFontOfSize(15)
        label.textColor = UIColor.grayColor()
        
        if let name = UserAccountViewModel.shareUserAccount.userAccount?.name {
            label.text = "欢迎回来,\(name)"
        } else {
            label.text = "欢迎回来"
        }
        
        //设置透明度
        label.alpha = 0
        
        return label
    }()
    
    override func loadView() {
        
        //自定义视图
        view = bgImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
    }
    
    override func viewDidAppear(animated: Bool) {
      super.viewDidAppear(animated)
        
        //执行动画
        startAnimation()
    }
    
    // 添加视图设置约束
    private func setupUI() {
        
        view.addSubview(headImageView)
        view.addSubview(messageLabel)
        
        headImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(view).offset(200)
            make.centerX.equalTo(view)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            make.centerX.equalTo(headImageView)
        }
    }
    
    
    //开启动画
    private func startAnimation() {
        self.headImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(self.view).offset(100)
        }
        
        //usingSpringWithDamping 阻尼, 取值范围是0-1,阻尼越大弹簧效果越小
        //initialSpringVelocity 初始速度
        //枚举不想传入直接输入[]
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            
            //执行约束动画
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(1, animations: { () -> Void in
                    
                    //设置透明度显示
                    self.messageLabel.alpha = 1
                    
                    }, completion: { (_) -> Void in
                        
                        //进入首页
                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootVCNotification, object: nil)
                        
                })
                
        }
    }
    
    
}
